{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 远程服务器操作界面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import requests\n",
    "import time\n",
    "\n",
    "def operate(cmd, path_1=None, path_2=None, ip='http://182.61.19.201:8899/'):\n",
    "    if cmd.startswith('nohup'):\n",
    "        cmd = cmd + '> {}_{}.log'.format(cmd.replace(' ', '_'),  time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())))\n",
    "    elif cmd.startswith('get') or cmd.startswith('put'):\n",
    "        cmd = '{}\\t{}\\t{}'.format(cmd, path_1.strip(), path_2.strip())\n",
    "\n",
    "    # 发送命令\n",
    "    api_response = requests.post(ip+'cmd', data= cmd.encode('utf-8'))\n",
    "    print(api_response.text, end='\\n\\n')        \n",
    "\n",
    "    # 等待返回结果 \n",
    "    for _ in range(100):\n",
    "        api_response = requests.post(ip+'res', data= cmd.encode('utf-8')).text\n",
    "        if api_response != 'None':\n",
    "            break\n",
    "        time.sleep(0.1)\n",
    "\n",
    "    print(api_response)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ls -lh .\n",
      "\n",
      "total 84K\n",
      "-rw-r--r-- 1 root root 4.2K Mar 20 14:38 RemoteOperation_Server.py\n",
      "-rw-r--r-- 1 root root  31K Mar 20 15:07 Untitled.ipynb\n",
      "-rw-r--r-- 1 root root  170 Mar 20 13:30 log2.txt\n",
      "-rw-r--r-- 1 root root   49 Mar 20 14:08 ls\n",
      "-rw-r--r-- 1 root root  269 Mar 20 14:08 ls_-lh_log2.txt\n",
      "-rw-r--r-- 1 root root  471 Mar 20 14:55 nohup_ls_-lh_20210320225545.log\n",
      "-rw-r--r-- 1 root root  544 Mar 20 14:58 nohup_ls_-lh_20210320225859.log\n",
      "-rw-r--r-- 1 root root  617 Mar 20 14:59 nohup_ls_-lh_20210320225943.log\n",
      "-rw-r--r-- 1 root root  690 Mar 20 15:00 nohup_ls_-lh_20210320230045.log\n",
      "-rw-r--r-- 1 root root  763 Mar 20 15:04 nohup_ls_-lh_20210320230410.log\n",
      "-rw-r--r-- 1 root root   12 Mar 20 13:24 test1010101.txt\n",
      "-rw-r--r-- 1 root root 4.5K Mar 20 15:08 远程SSH.ipynb\n",
      "\n",
      "pwd\n",
      "\n",
      "/notebooks/Desktop/_temp\n",
      "\n"
     ]
    }
   ],
   "source": [
    "operate('ls -lh .')\n",
    "operate('pwd')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nohup ls -lh> nohup_ls_-lh_20210320230902.log\n",
      "\n",
      "BackOrder: nohup ls -lh> nohup_ls_-lh_20210320230902.log\n"
     ]
    }
   ],
   "source": [
    "# 后台操作\n",
    "operate('nohup ls -lh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "put\t/notebooks/Desktop/_temp/log2.txt\t/root/远程操作\n",
      "\n",
      "Put Fils  /notebooks/Desktop/_temp/log2.txt -> /root/远程操作\n"
     ]
    }
   ],
   "source": [
    "# 远程服务器上传文件\n",
    "operate('put', '/notebooks/Desktop/_temp/log2.txt ', '/root/远程操作 ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "get\t/root/远程操作/远程SSH.ipynb\t/notebooks/Desktop/_temp\n",
      "\n",
      "Get Fils  /root/远程操作/远程SSH.ipynb -> /notebooks/Desktop/_temp\n"
     ]
    }
   ],
   "source": [
    "# 远程服务器下载文件\n",
    "operate('get', '/root/远程操作/远程SSH.ipynb', '/notebooks/Desktop/_temp')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rm nohup_l*\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "operate('rm nohup_l*')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
